package main

import (
	"fmt"
	"net"
)

type workload interface {
	do() error
}

type coreWorkloadCreator struct {	
}
type coreWorkload struct {
}

func (coreWorkload) do() error { return nil }
// 如果do 是指针版本，则下面的 Create 就必须写成 new(coreWorkload) 的形式; 如果不是指针版本，则两种方式都可以
/////// 如何解释？？？
//func (c *coreWorkload) do() error { return nil }

func (coreWorkloadCreator) Create() (workload, error) {
	// 这里为何不能是 coreWorkload ??? 难道非得返回指针给接口类型？
	c := new(coreWorkload)
	//c := coreWorkload{}

	return c, nil
}

/////// 还有个情况是我写的trpc-benchmark 的metricsExposer如果写成了非指针的形式，则编译报错！！！查看一下原因

///////////////////////
// https://stackoverflow.com/questions/51611086/pointer-to-interface-is-not-an-interface-error
type brace interface{}

type round struct {
	prev *brace // error 1
	prev2 brace
}

type square struct {
	len int
}

func tStructToInterface() {
	var r round
	s := square{1}
	fmt.Printf("square addr: %v\n", &s)
	// cannot use s (variable of type square) as *brace value in assignment
	//  although a pointer to a concrete type can satisfy an interface, with 
	//    one exception a pointer to an interface can never satisfy an interface
	//r.prev = s // error 1
	//r.prev = &s // error too

	r.prev2 = &s // ok
	fmt.Println(r.prev2) // &{1}
	r.prev2 = s
	fmt.Println(r.prev2) // {1}
}

////////////// 将 net.Conn 包进 countingConn中，并只实现其中两个方法，竟然可以这样用？？
type countingConn struct {
	net.Conn
	byteRead, byteWritten int64
}

func (cc *countingConn) Read(b []byte) (int, error) {
	return 0, nil
}
func (cc *countingConn) Write(b []byte) (int ,error) {
	fmt.Println("countingConn Write called")
	return 0, nil
}

func tOnlyImplementSomeMethodsOfInterface() {
	var conn net.Conn
	c := &countingConn{byteRead: 1}
	conn = c // 可以这样赋值 ??
	conn.Write([]byte("abc"))
	conn.Close() // 这调的是谁的 Close 方法？
}
